/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   TrafficSurveyServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz    </p>
 * 
 * <p>最后修改 : $: 2017年8月1日-下午7:31:10 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.informatization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jsbz.informatization.TrafficSurveyDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.TrafficAdjust;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.informatization.TrafficSurveyService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * TrafficSurveyServiceImpl
 * 交调管理的表内容管理
 * 2017年8月1日 下午7:31:10
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class TrafficSurveyServiceImpl implements TrafficSurveyService {
	
	@Autowired
	private TrafficSurveyDao trafficSurveyDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	@Override
	public List<TrafficAdjust> getTrafficSurveyDatas(QueryFilter queryFilter){
		return trafficSurveyDao.find(queryFilter, "year", true);
	}
	
	@Override
	public Map<String, Object> getTrafficSurvey(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<TrafficAdjust> trafficList = new ArrayList<TrafficAdjust>();
		List<Object> values = new ArrayList<Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = checkTable.getYearFull() == null? Double.parseDouble((String)jsonObject.get("yearFull")) : checkTable.getYearFull();
		double yearPart = checkTable.getYearPart() == null? Double.parseDouble((String)jsonObject.get("yearPart")) : checkTable.getYearPart();
		double poitMark = checkTable.getYearPart() == null? Double.parseDouble((String)jsonObject.get("poitMark")) : checkTable.getPoitMark();
		double reserveMark = checkTable.getYearPart() == null? Double.parseDouble((String)jsonObject.get("reserveMark")) : checkTable.getReserveMark();
		double firstStepScore = checkTable.getFirstStepScore() == null? Double.parseDouble((String)jsonObject.get("firstStepScore")) : checkTable.getFirstStepScore();
		
		String stationName = (String)jsonObject.get("stationName");
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, String.valueOf(Integer.parseInt(endTime)-1), tag);
		
		//养护资金项目的名称
		QueryFilter queryFilter = new QueryFilter();
		//年度养护资金详情查询
		queryFilter.addFilter("year", startTime, MatchType.GE);
		queryFilter.addFilter("year", String.valueOf(Integer.parseInt(endTime)-1), MatchType.LE);
		double total =0; 		//检查得分
		double percentage =0; 	//系统完善度
		double tableFull  = 0;	//满分
		//项目资金总览()
		if (StringUtils.isEmpty(stationName)) {
			trafficList = this.getTrafficSurveyDatas(queryFilter);
		} else {
			queryFilter.addFilter("stationName", stationName, MatchType.EQ);
			trafficList = this.getTrafficSurveyDatas(queryFilter);
			//返回时间
			int years[] = new int[Integer.parseInt(endTime) - Integer.parseInt(startTime)];
			for (int i = 0; i < years.length; i++) {
				years[i] = Integer.parseInt(startTime)+i;
			}
			//计算得分详情和资料完善度
			//资金项目使用比例得分
			//1表示当前年份有数据，0表示当前年份无数据
			List<String> timeResult = new ArrayList<String>(years.length);
			for ( int i = 0; i<years.length; i++){
				for (TrafficAdjust trafficAdjust : trafficList) {
					if (years[i] == Integer.parseInt(trafficAdjust.getYear())) {
						if (timeResult.size() <= i) {
							timeResult.add("1");
						}
						if ("0".equals(timeResult.get(i))) {
							timeResult.set(i, "1");
						}
						break; 
					} else {
						if (timeResult.size() <= i) {
							timeResult.add("0");
						}
					}
				}
			}
			map.put("timeResult", timeResult);
			
			
			if (trafficList.size() > 0){
				if (list.size() >0 ){
					if (trafficList.get(trafficList.size()-1).getStationConstruction().equals("1")) {
						total = MathsUtils.sumDouble(total, yearFull);
						trafficList.get(trafficList.size()-1).setAttMark(yearFull);
					}
				} else {
					total = MathsUtils.sumDouble(total, yearPart);
					trafficList.get(trafficList.size()-1).setAttMark(yearPart);
				}
				if (trafficList.get(trafficList.size()-1).getNormalRun().equals("1") && trafficList.get(trafficList.size()-1).getDataComplete().equals("1")) {
					total = MathsUtils.sumDouble(total, reserveMark);
					trafficList.get(trafficList.size()-1).setRunMark(reserveMark);
				} else if (trafficList.get(trafficList.size()-1).getDataComplete().equals("0") && trafficList.get(trafficList.size()-1).getNormalRun().equals("1")) {
					total = MathsUtils.sumDouble(total, firstStepScore);
					trafficList.get(trafficList.size()-1).setRunMark(firstStepScore);
				} else {
					trafficList.get(trafficList.size()-1).setRunMark(0.0);
				}
			}
			for (TrafficAdjust trafficAdjust : trafficList) {
				if (trafficAdjust.getStationAnalyze().equals("1") && trafficAdjust.getYearReport().equals("1")) {
					total = MathsUtils.sumDouble(total, poitMark);
					trafficAdjust.setYearMark(poitMark);
				} else {
					trafficAdjust.setYearMark(0.0);
				}
				
			}
			
			map.put("projectMark", total);
			map.put("sum",  MathsUtils.formatDouble(total,1));
			//判断完善度
			tableFull = MathsUtils.mulDouble(poitMark, years.length);
			tableFull = MathsUtils.sumDouble(yearFull, tableFull);
			tableFull = MathsUtils.sumDouble(reserveMark, tableFull);
			tableFull =	MathsUtils.formatDouble(tableFull,2);
			percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
			map.put("percent", percentage);
			map.put("years", years);
			
			values.add(stationName);
		}
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("overView", trafficList);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
